//
// PcwTextEditor.cs
//  AddIn root
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Aug 9, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.IO;
using Gtk;
using Mono.Addins;
using Pcw.PcwTextEditor.PcwDocument;

// This library is the main add-in root
//[assembly:AddinRoot ("Core", "1.0", Namespace="PcwTextEditor")]

namespace Pcw.PcwTextEditor
{
	public class TextEditor
	{
		public TextEditor ()
		{
			InitAddins();
		}
		
		// Usefull when testing without calling the gui
		public static void InitAddins()
		{
			PcwLog.WriteLog(PcwLogLevel.DEBUG, "InitAddins", "Init Pcw.Texteditor");
			
			// Initialize the add-in engine
			AddinManager.Initialize (".");
			AddinManager.Registry.Update ();			
		}
		
		/// <summary>
		/// Open the editor with a new blank document (a FileDocument object).
		/// </summary>
		public bool Open()
		{
			return Open(new List<IDocumentType>(), "");
		}
		
		/// <summary>
		/// Open the editor with an IDocumentType Object, usually a DirectText object.
		/// </summary>
		/// <param name="doc">
		///   <see cref="IDocumentType"/> object to be manipulated by the editor.
		/// </param>
		public bool Open(IDocumentType doc)
		{
			List<IDocumentType> docs = new List<IDocumentType>();
			docs.Add(doc);
			return Open(docs, "");
		}
		
		/// <summary>
		/// Open the editor with an IDocumentType Object, usually a DirectText object.
		/// </summary>
		/// <param name="doc">
		///   <see cref="IDocumentType"/> to be manipulated by the editor.
		/// </param>
		/// <param name="rcFile">
		///   <see cref="String"/> representing an rc file for custom style.
		/// </param>
		public bool Open(IDocumentType doc, string rcFile)
		{
			List<IDocumentType> docs = new List<IDocumentType>();
			docs.Add(doc);			
			return Open(docs, rcFile);
		}
		
		/// <summary>
		/// Open the editor with a list of documents (FIlesDocument type).
		/// </summary>
		/// <param name="files">
		///   <see cref="List<string>"/> List of files to Open.
		/// </param>
		/// <param name="rcFile">
		///   <see cref="String"/> representing an rc file for custom style.
		/// </param>
		public bool Open(List<string> files, string rcFile)
		{
			List<IDocumentType> docs = new List<IDocumentType>();
			foreach (string f in files)
				docs.Add(new FileDocument(f));
			
			return Open(docs, rcFile);

		}
		
		private bool Open(List<IDocumentType> docs, string rcFile)
		{
			// Rc file for custom style
			if (!String.IsNullOrEmpty(rcFile)) {
				if(File.Exists(rcFile))
					Rc.Parse(rcFile);
			}
			
			Application.Init();
			MainWin = new MainWindow();
			MainWin.Initialize (docs);
			Application.Run();
			
			return true;
		}
		
		public static string GetStringLine(TextBuffer buffer, 
			int startLine, ref TextIter startIter, 
			int endLine, ref TextIter endIter) 
		{			
			TextIter sIter = buffer.GetIterAtLine(startLine);
			TextIter eIter;
			if (endLine == buffer.LineCount - 1) {
				eIter = buffer.EndIter;
			}
			else {
				eIter = buffer.GetIterAtLine(endLine + 1);
				eIter.BackwardChar();
			}
			
			startIter = sIter;
			endIter = eIter;
			
			return buffer.GetText(sIter, eIter, true);
		}
		
		public static string GetStringOfsset(TextBuffer buffer, 
			int startOffset, ref TextIter startIter, 
			int endOffset, ref TextIter endIter) 
		{	
			TextIter sIter = buffer.StartIter;
			sIter.ForwardChars(startOffset);
			TextIter eIter = buffer.StartIter;
			eIter.ForwardChars(endOffset);
			
			startIter = sIter;
			endIter = eIter;
			
			return buffer.GetText(sIter, eIter, true);
		}
		
		public static void RemmoveAllTagsFromOffset(TextBuffer buffer, int startOffset, int endOffset) 
		{
			TextIter sIter = buffer.StartIter;
			sIter.ForwardChars(startOffset);
			TextIter eIter = buffer.StartIter;
			eIter.ForwardChars(endOffset);
			
			buffer.RemoveAllTags(sIter, eIter);
		}
		
		/// <summary>
		/// The main window of the text editor
		/// </summary>
		/// <remarks>
		/// This property exposes the Main Window. You can
		/// get the CurrDocument or the list of Documents opened
		/// by the application.
		/// </remarks>
		public static MainWindow MainWin { get; private set; }		
	}
}

